import axios from 'axios';
import Cookie from 'js-cookie'
import store from '../store/index'


const BaseUrl = 'http://localhost:3000/'
export const Service = axios.create({
  baseURL: BaseUrl,
})

Service.interceptors.request.use((config) => {
  let token = sessionStorage.getItem('Authorization')
    if (!config.headers.hasOwnProperty('Authorization') && token) {
      config.headers.Authorization = token;
    }
    return config;
}, (err) => {
  return Promise.reject(err)
})

Service.interceptors.response.use((res) => {
  let { status, data  } = res
  if (status === 200) {
    data.token ? sessionStorage.setItem('Authorization', data.token) : ''
    if (data.code === 10001 || data.code === '10001') {
      sessionStorage.clear()
      this.$Message['error']({
        background: true,
        content: '登录状态过期,请重新登录'
      })
    }
    return Promise.resolve(data)
  }
}, (err) => {
  return Promise.reject(err)
})